﻿using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Windows.Forms;

namespace DataVisualizers
{
    /// <summary>
    /// A Visualizer for a 1D array contained in a wrapper class.  
    /// </summary>
    public class ArrayWrapperVisualizer : DialogDebuggerVisualizer
    {
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            // TODO: Get the object to display a visualizer for.
            //       Cast the result of objectProvider.GetObject() 
            //       to the type of the object being visualized.
            var data = ((ArrayWrapper)objectProvider.GetObject()).InnerArray;
            int widthGuess = data.Length / (data.Length / ((int)Math.Sqrt(data.Length)));

            // TODO: Display your view of the object.
            //       Replace displayForm with your own custom Form or Control.)
            using (Form displayForm = new ArrayForm(data, widthGuess))
            {
                displayForm.Text = "Array Wrapper for double[], length: " + data.Length;

                windowService.ShowDialog(displayForm);
            }
        }

        /// <summary>
        /// Tests the visualizer by hosting it outside of the debugger.
        /// </summary>
        /// <param name="objectToVisualize">The object to display in the visualizer.</param>
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(ArrayWrapperVisualizer));
            visualizerHost.ShowVisualizer();
        }
    }
}
